package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.dbRole;
import com.example.demo.pojo.dbUser;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;

    @Override
    public Result login(String username, String userpwd, HttpServletRequest request) {
        dbUser user = userMapper.selectOne(username,userpwd);
        if (user==null){
            return Result.fail("用户名或密码错误");
        }
        dbRole userrole = userMapper.selectEdit(user.getID());
        HttpSession session = request.getSession();
        session.setAttribute("user_edit_top", userrole.getEdit_top());
        session.setAttribute("user_edit_middle", userrole.getEdit_middle());
        session.setAttribute("user_edit_below", userrole.getEdit_below());
        session.setAttribute("user_edit_admin", user.isDAdmin());
        session.setAttribute("user_name", user.getDName());
        return Result.success("成功");
    }

    @Override
    public Result select() {
        List<dbUser> select = userMapper.select();
        return Result.success(select);
    }

    @Override
    public Result adminlogin(String username, String userpwd, HttpServletRequest request) {
        dbUser user = userMapper.selectAdmin(username,userpwd);
        if (user==null){
            return Result.fail("用户名或密码错误");
        }
        HttpSession session = request.getSession();
        session.setAttribute("admin_name", user.getDName());
        return Result.success("成功");
    }
}
